package com.shuai.jit.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shuai.jit.entity.Menu;
import com.shuai.jit.entity.R;
import com.shuai.jit.entity.user.Moment;
import com.shuai.jit.entity.user.User;
import com.shuai.jit.entity.vo.VoMoment;
import com.shuai.jit.entity.vo.VoUser;
import com.shuai.jit.mapper.MenuMapper;
import com.shuai.jit.mapper.MomentMapper;
import com.shuai.jit.mapper.UserMapper;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@Api("动态控制类")
@RequestMapping("/moment")
public class MomentController {

    @Autowired
    MomentMapper momentMapper;

    @Autowired
    UserMapper userMapper;

    /**
     * 获取动态
     * @return
     */
    @RequestMapping("/getMoment")
    public R getMoment(){
        List<VoMoment> momentList=new ArrayList<>();
        List<Moment> moments=momentMapper.selectList(null);
        for (Moment moment:moments){
            User user=userMapper.getUserInfo(moment.getUserId());
            momentList.add(new VoMoment(moment,user));
        }
        return R.ok().data("moment",momentList);
    }

    @RequestMapping("/addMoment")
    public R addMoment(@RequestBody Moment moment){

        if(moment==null|| moment.getUserId()==null){
            return R.error().data("moment","对不起动态数据为空");
        }
        Integer integer=momentMapper.insert(moment);
        if(integer>0){
            return R.ok().data("moment","动态添加成功");
        }else {
            return R.ok().data("moment","动态添加失败");
        }
    }
}
